package com.ddc.study.feature.login;

import android.text.TextUtils;

import com.ddc.study.R;
import com.ddc.study.base.BaseApplication;
import com.ddc.study.entity.User;
import com.ddc.study.net.NetworkUtil;
import com.ddc.study.net.response.BaseResponse;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created on 2018/3/28.
 *
 * @author lshun
 * @version 1.0
 */
public class LoginPresenter extends LoginContract.IPresenter {
    @Override
    public void login(@Nullable final String username, @Nullable final String password) {
        if (TextUtils.isEmpty(username)) {
            getView().showUsernameError(getView().getStringValue(R.string.error_field_required));
            return;
        } else if (!isUsernameValid(username)) {
            getView().showUsernameError(getView().getStringValue(R.string.error_invalid));
            return;
        }

        if (TextUtils.isEmpty(password)) {
            getView().showPasswordError(getView().getStringValue(R.string.error_field_required));
            return;
        } else if (!isPasswordValid(password)) {
            getView().showPasswordError(getView().getStringValue(R.string.error_invalid));
            return;
        }

        //show dialog
        getView().showDialog(getView().getStringValue(R.string.login_ing));

        NetworkUtil.login(username, password).enqueue(new Callback<BaseResponse<User>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<User>> call,
                                   @NonNull Response<BaseResponse<User>> response) {
                if (!isViewAttached()) return;
                BaseResponse<User> body = response.body();
                if (body == null) {
                    getView().loginFailed(getView().getStringValue(R.string.response_empty_body));
                } else if (!body.isSuccess()) {
                    getView().loginFailed(body.getErrorMsg());
                } else {
                    getView().loginSuccess(body.getData().getCollectIds());
                    //通知User更新了
                    BaseApplication.getInstance().setUser(body.getData());
                }
                getView().dismissDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<User>> call,
                                  @NonNull Throwable t) {
                if (!isViewAttached()) return;
                getView().loginFailed(getView().getStringValue(R.string.network_request_failed));
                getView().dismissDialog();
            }
        });
    }

    @Override
    public void register(@Nullable String username, @Nullable String password,
                         @Nullable String rePassword) {
        if (TextUtils.isEmpty(username)) {
            getView().showUsernameError(getView().getStringValue(R.string.error_field_required));
            return;
        } else if (!isUsernameValid(username)) {
            getView().showUsernameError(getView().getStringValue(R.string.error_invalid));
            return;
        }

        if (TextUtils.isEmpty(password)) {
            getView().showPasswordError(getView().getStringValue(R.string.error_field_required));
            return;
        } else if (!isPasswordValid(password)) {
            getView().showPasswordError(getView().getStringValue(R.string.error_invalid));
            return;
        }

        //TODO 校验rePassword

        //show dialog
        getView().showDialog(getView().getStringValue(R.string.register_ing));

        NetworkUtil.register(username, password, rePassword).enqueue(new Callback<BaseResponse<User>>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponse<User>> call,
                                   @NonNull Response<BaseResponse<User>> response) {
                BaseResponse<User> body = response.body();
                if (!isViewAttached()) return;
                if (body == null) {
                    getView().registerFailed(getView().getStringValue(R.string.response_empty_body));
                } else if (!body.isSuccess()) {
                    getView().registerFailed(body.getErrorMsg());
                } else {
                    getView().registerSuccess();
                    //通知User更新了
                    BaseApplication.getInstance().setUser(body.getData());
                }
                getView().dismissDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseResponse<User>> call,
                                  @NonNull Throwable t) {
                if (!isViewAttached()) return;
                getView().registerFailed(getView().getStringValue(R.string.network_request_failed));
                getView().dismissDialog();
            }
        });
    }

    private boolean isUsernameValid(String username) {
        return username.length() > 4;
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }
}
